/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sp.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Ristika Nugraha [12.7350]
 */
public class LogDatabase {

    public static void write(String host, String db, String user, String pw) {
        Date date = new Date();
        File file = new File("JPA.pkl54");
        try (FileWriter output = new FileWriter(file, false)) {
            output.write(Enkrip.encrypt(host + ":" + db + ":" + user + ":" + pw + ":" + date.toString()));
        } catch (IOException ex) {
            Logger.getLogger(LogDatabase.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(LogDatabase.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void readLog() throws IOException, Exception {
        try (BufferedReader input = new BufferedReader(new FileReader("JPA.pkl54"))) {
            String str = null;
            StringBuilder sb = new StringBuilder();
            while (true) {
                String inputText = input.readLine();
                if (inputText == null) {
                    break;
                }
                sb.append(inputText);
            }
            str = Enkrip.decrypt(sb.toString());
            String[] auth = str.split(":");
            UserControl.setAuth(auth);
        }
    }

}
